package com.feel.modules.auth.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.feel.common.exception.AuthException;
import com.feel.common.redis.RedisUtils;
import com.feel.common.utils.R;
import com.feel.config.JedisNameConstants;
import com.feel.modules.app.entity.UserEntity;
import com.feel.modules.app.vo.LoginVo;
import com.feel.modules.app.vo.RegisterVo;
import com.feel.modules.auth.service.UserService;
import com.feel.modules.security.service.UserCurrentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Api("用户登录注册")
@Slf4j
public class LoginController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCurrentService userCurrentService;

    @GetMapping("/current")
    @ApiOperation(value = "获取当前登录用户 ", notes = "获取当前登录用户")
    public R<UserEntity> current() {
        UserEntity fUserEntity = userCurrentService.getUserCurrent().getUserEntity();
        return R.ok(fUserEntity);
    }


    @PostMapping("/register")
    @ApiOperation(value = "用户注册 ", notes = "用户注册")
    public R<UserEntity> register(@RequestBody RegisterVo registerVo, @RequestHeader HttpHeaders httpHeaders) {
        log.info("用户注册 {}", registerVo.toString());
        String verificationCode = redisUtils.get(JedisNameConstants.USER_CODE + registerVo.getUsername());
        if (ObjectUtils.isEmpty(registerVo.getVerificationCode())
                || !verificationCode.equals(registerVo.getVerificationCode())
        ) {
            throw new AuthException(R.error("验证码错误!"));
        }
        return R.ok(userService.createUserByPhone(registerVo, httpHeaders));
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录 ", notes = "用户登录")
    public R<UserEntity> login(@RequestBody LoginVo loginVo, @RequestHeader HttpHeaders httpHeaders) {
        log.info("用户登录", loginVo.toString());
        String username = loginVo.getUsername();
        if (ObjectUtil.isNotEmpty(loginVo.getPassword())) {
            UserEntity userEntity = userService.checkUser(username, loginVo.getPassword());
            return R.ok(userEntity);
        } else if (ObjectUtil.isNotEmpty(loginVo.getVerificationCode())) {
            UserEntity userEntity = userService.checkUserByVerificationCode(username, loginVo.getVerificationCode());
            return R.ok(userEntity);
        }
        return R.error("用户名或密码错误");
    }

    @GetMapping("/verificationCode")
    @ApiOperation(value = "获取验证码 ", notes = "获取验证码")
    public R<Boolean> getVerificationCode(@RequestParam("phone") String phone) {
        String code = RandomUtil.randomNumbers(6);
        log.info("发送验证码，手机号 {}, code {}", phone, code);

        redisUtils.set(JedisNameConstants.USER_CODE + phone, code);
        //TODO 上线需要修改
        return R.ok(code);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录 ", notes = "退出登录")
    public R<Boolean> logout() {
        StpUtil.logout();
        return R.ok();
    }
}
